<?php

declare (strict_types = 1);

namespace app\api\model\user;

use app\api\service\User as UserService;
use app\common\enum\PointsType;
use app\common\model\user\PointsLog as PointsLogModel;

/**
 * 用户积分变动明细模型
 * Class PointsLog
 * @package app\api\model\user
 */
class PointsLog extends PointsLogModel
{
    protected $hidden = [
        'store_id',
        'created_at',
        'updated_at'
    ];
    /**
     * 获取日志明细列表
     */
    public function getList($params): array
    {
        // 当前用户ID
        $userId = UserService::getCurrentLoginUserId();
        // 获取列表数据
        return $this->where('user_id', '=', $userId)
            ->order(['created_at' => 'desc'])
            ->paginate($params['limit'] ?? 15)
            ->each(function (&$item){
                if($item['value'] > 0){
                    $item['value'] = '+'.$item['value'];
                }
                $item['describe'] = PointsType::data()[$item['scene']]['name'];
            })
            ->toArray();
    }

    public function setPointsLog($scene,$user_id,$points,$remark = ''): bool
    {

        return (new static())->save([
            'scene' => $scene,
            'user_id' => $user_id,
            'value' => $points,
            'describe' => PointsType::data()[$scene]['name'],
            'remark' => $remark,
        ]);
    }

}